<?php
/**
 * ScrapperHelper
 * Scrapping content helpers
 *
 * @author Endel Dreyer
 */
class ScrapperHelper {

	/**
	 * Get array of all links on subject
	 *
	 * Result example:
	 *	array(
	 *		'Link label' => 'http://link_to_somewhere.com',
	 *		'Link label' => 'http://link_to_somewhere.com',
	 *		'Link label' => 'http://link_to_somewhere.com'
	 *	)
	 */
	public static function links($subject) {
		preg_match_all('/<a[^>]+href\s*=\s*["\'](?!(?:#|javascript\s*:))([^"\']+)[^>]*>(.*?)<\/a>/si', $subject, $matches);

		$links = array();
		for($i=0;$i<count($matches[0]);$i++)
		{
			$links[$matches[1][$i]] = $matches[2][$i];
		}
		return $links;
	}

	/**
	 * Get array of all emails on subject
	 *
	 * Result example:
	 *	array(
	 *		'some@email.address' => '2', // email => number of occurrences
	 *		'some2@email.address' => '1',
	 *		'some3@email.address' => '4'
	 *	)
	 */
	public static function emails($subject) {
		preg_match_all('/[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]/', $subject, $matches);

		$addresses = array();
		foreach ($matches[0] as $email_address)
		{
			$new_address = !isset($addresses[$email_address]);
			$addresses[$email_address] = ($new_address) ? 1 : $addresses[$email_address] + 1;
		}
		
		return $addresses;
		
	}

}

?>
